package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/validate-binary-search-tree/
 * @create 2025/9/15 20:47
 * @since JDK17
 */

public class Solution48 {
    boolean ans = true;
    int[] dfs(TreeNode root) {
        if (root == null) {
            return new int[]{Integer.MIN_VALUE, Integer.MAX_VALUE};
        }
        int[] l = dfs(root.left), r = dfs(root.right);
        if (l[0] >= root.val || r[1] <= root.val) { // 左边的最大值，右边的最小值
            ans = false;
        }
        l[0] = Math.max(l[0], root.val);
        r[1] = Math.min(r[1], root.val);
        return new int[]{l[0], r[1]};
    }

    public boolean isValidBST(TreeNode root) {
        dfs(root);
        return ans;
    }
}
